#include "Player.h"

#include <iostream>

Player::Player(): delay(sf::milliseconds(20)), addX (500), addY(500), sourceX(0), sourceY(0), speed(0), moving(false)
{
	walkingright.loadFromFile("Assets/prcolor.png");

	rect.setTextureRect(sf::IntRect(0, 0, 500, 500));//Setting the animation to the first frame to start with.
	rect.setSize(sf::Vector2f(100, 100));//Setting the player size.
	rect.setPosition(336, 236);//Setting the player position.
	rect.setTexture(&walkingright);//Setting the texture to walkingright.
}

void Player::startmove(Player::Direction direction)
{
	speed = 0;
	moving = true;
	this->direction = direction;
}

void Player::stopmove()
{
	speed = 0;
	moving = false;
	rect.setTextureRect(sf::IntRect(0,0,500,500));
}

void Player::update(const sf::Time& elapsed) 
{
	if(speed != 0)
		delay -= elapsed;

	if(delay < sf::milliseconds(0) && direction == Direction::Right || moving == true){
		delay += sf::milliseconds(20);
		rightMove();
	}
}

void Player::rightMove()
{
	if (sourceX == addX*11)	
	sourceX = 0;

	rect.setTextureRect(sf::IntRect(sourceX, sourceY, addX, addY));

	sourceX += addX;
	speed = 0;
}

void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(rect, states);
}